package com.study.callable;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;



class CallableDownLoad implements Callable<Boolean> {
    private String url;
    private String name;

    public CallableDownLoad(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public Boolean call() throws Exception {
        WebDownLoad downLoad = new WebDownLoad();
        downLoad.downLoadImg(url, name);
        System.out.println("下载成功:" + name);
        return true;
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableDownLoad load = new CallableDownLoad("https://bkimg.cdn.bcebos.com/pic/d043ad4bd11373f082022223f9445cfbfbedab64ae7e?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U5Mg==,g_7,xp_5,yp_5/format,f_auto", "李.jpg");
        CallableDownLoad load1 = new CallableDownLoad("https://bkimg.cdn.bcebos.com/pic/d043ad4bd11373f082022223f9445cfbfbedab64ae7e?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U5Mg==,g_7,xp_5,yp_5/format,f_auto", "浩.jpg");
        CallableDownLoad load2 = new CallableDownLoad("https://bkimg.cdn.bcebos.com/pic/5d6034a85edf8db1cb133d2e1868ca54564e9258de6c?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UxNTA=,g_7,xp_5,yp_5/format,f_auto", "存.jpg");
        //创建执行服务
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        // 提交执行
        Future<Boolean> submit = threadPool.submit(load);
        Future<Boolean> submit1 = threadPool.submit(load1);
        Future<Boolean> submit2 = threadPool.submit(load2);
       // 获取结果
        Boolean aBoolean = submit.get();
        Boolean aBoolean1 = submit1.get();
        Boolean aBoolean2 = submit2.get();
        //关闭服务
        threadPool.shutdownNow();
    }

}


/**
 * 
 */
class WebDownLoad{
    public void downLoadImg (String url,String name) {
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("io 异常 downLoadImg出现错误");
        }
    }
}
